package leetcode.sort;

/**
 * @author way
 * @create 2025-03-19 10:05
 * 只要当前i对应的数是0或者2，就去交换，但是不动i的位置
 * 只有i既不是0也不是2的时候，再移动i
 */
public class _75_sortColors {
    public void sortColors(int[] nums) {
        ////[0...zero]是0,[two...len-1]是2
        int zero = 0;
        int two = nums.length - 1;
        int i = 0;
        while (i <= two) {
            int tmp;
            if (nums[i] == 0) {
                tmp = nums[zero];
                nums[zero] = nums[i];
                nums[i] = tmp;
                zero++;
                i++;
            } else if (nums[i] == 2) {
                tmp = nums[i];
                nums[i] = nums[two];
                nums[two] = tmp;
                two--;
            } else {
                i++;
            }
        }
    }
}
